﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Cartoon.Logic;
using Cartoon.Model;
using Cartoon.Skin;

namespace CartoonViewer
{
    /// <summary>
    /// W_ChapterPictureViewer.xaml 的交互逻辑
    /// </summary>
    public partial class W_ChapterPictureViewer : Window
    {
        public W_ChapterPictureViewer()
        {
            InitializeComponent();
			this.WindowState = WindowState.Maximized;
        }
        public ISkinService SkinS;
        Chapter _chapter;
        ICartoonView _cartoonView;
        List<BitmapImage> images = new List<BitmapImage>();
        int LookIndex = 0;
        int ImageCount = 0;
        System.Windows.Threading.DispatcherTimer _showTimer;
        bool IsRight;
        double _showWidth;
        double _showSpeed;
        double _ImgWidth;
        public W_ChapterPictureViewer(ICartoonView cv,  ISkinService _SkinS,Chapter c)
            : this()
        {

            this.SkinS = _SkinS;
            this.SkinS.Changed += new Action(_SkinS_Changed);
            LoadBackGround();
            _chapter = c;
            _cartoonView = cv;
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            this.Title = this._chapter.comic.Name + "第" + this._chapter.Id + "话";

            string picSavePath = "";
            if (this._cartoonView.CartoonSavePath.EndsWith("\\") == false || this._cartoonView.CartoonSavePath.EndsWith("/"))
            {
                this._cartoonView.CartoonSavePath += "\\";
            }
            picSavePath = this._cartoonView.CartoonSavePath + _chapter.comic.Name + "\\" + _chapter.Id + "\\";
            picSavePath = new System.IO.DirectoryInfo(picSavePath).FullName + "\\";
             
             
             
            foreach (string imgname in this._chapter.PicListUrl)
            { 
                images.Add(new BitmapImage(new Uri(picSavePath + System.IO.Path.GetFileName( imgname),UriKind.RelativeOrAbsolute)));
            }

            ImageCount = images.Count;
            this.ImgShow.Source = images[LookIndex];
			this.SetTitle();
            _showTimer = new System.Windows.Threading.DispatcherTimer();
            _showTimer.Tick += new EventHandler(_showTimer_Tick);
            _showTimer.Interval = new TimeSpan(0, 0, 0, 0, 2);
            _showSpeed = 15;
            _showWidth = 0;
            _ImgWidth = 760;
            IsRight = true;
            
        }

     

        void _SkinS_Changed()
        {
            this.LoadBackGround();
        }
        ImageBrush minNormal;
        ImageBrush minMouseover;
        ImageBrush closeNormal;
        ImageBrush closeMouseover;
        ImageBrush mainBackGround;
        private void LoadBackGround()
        {
            //ImageBrush b = new ImageBrush();
            //b.ImageSource = this.SkinS.Skin.BackGround;

            //mainBackGround = b;
            //this.Win.Background = mainBackGround;

            //minNormal = new ImageBrush(this.SkinS.Skin.MinNormal);
            //minMouseover = new ImageBrush(this.SkinS.Skin.MinMouseOver);
            //closeNormal = new ImageBrush(this.SkinS.Skin.CloseNormal);
            //closeMouseover = new ImageBrush(this.SkinS.Skin.CloseMouseOver);

            //this.LbMinWin.Background = minNormal;

            //this.LbCloseWin.Background = closeNormal;
        }
        void _showTimer_Tick(object sender, EventArgs e)
        {
            double translateXTemp = 0;
            double translateX = 0;
            if (this.IsRight == true)
            {
                translateXTemp = this.TranslateTransformForImgShowTemp.X + _showSpeed;
                translateX = this.TranslateTransformForImgShow.X + _showSpeed;
            }
            else
            {
                translateXTemp = this.TranslateTransformForImgShowTemp.X - _showSpeed;
                translateX = this.TranslateTransformForImgShow.X - _showSpeed;
            }

            this.TranslateTransformForImgShowTemp.X = translateXTemp;
            this.TranslateTransformForImgShow.X = translateX;
            _showWidth += _showSpeed;

            if (_showWidth >= _ImgWidth)
            {//结束切换动画 
                this.TranslateTransformForImgShow.X = 0;
                this.ImgShow.Source = this.ImgShowTemp.Source;
                this.PicPanelTemp.Visibility = Visibility.Hidden;
                this.ScalTransformForImgShow.ScaleX = 1;
                this.ScalTransformForImgShow.ScaleY = 1;
                this.ScrollViewForPic.ScrollToHome();
                this._showTimer.Stop();
				this.LookChanged();
            }


        }

		
		
        //上一张图片
        private void BtnLeft_Click(object sender, RoutedEventArgs e)
        {
            this.Preview();
        }

        //下一张图片
        private void BtnRight_Click(object sender, RoutedEventArgs e)
        {
           this.Next();
        }

		
		private void Preview()
		{
			if (LookIndex == 0)
            {
                LookIndex = ImageCount - 1;
            }
            else
            {
                LookIndex--;
            }

            this.ImgShowTemp.Source = images[LookIndex];
             

            IsRight = false;
            _showWidth = 0;
            this.PicPanelTemp.Visibility = Visibility.Visible;
            this.TranslateTransformForImgShowTemp.X =  _ImgWidth;
            this.TranslateTransformForImgShow.X = 0;

            this.ScalTransformForImgShow.ScaleX = 1;
            this.ScalTransformForImgShow.ScaleY = 1;
            _showTimer.Start();
		}
		
		private void Next()
		{
			LookIndex = ++LookIndex % ImageCount;
            this.ImgShowTemp.Source = images[LookIndex];
            _showWidth = 0;
            this.TranslateTransformForImgShowTemp.X = 0 - _ImgWidth;
            this.PicPanelTemp.Visibility = Visibility.Visible;
            
            IsRight = true;

            this.ScalTransformForImgShow.ScaleX =1;
            this.ScalTransformForImgShow.ScaleY = 1;

            _showTimer.Start();
		}
  
		
		private void LookChanged()
		{
			this.SetTitle();
		}
		
		private void SetTitle()
		{
			this.Title = this._chapter.comic.Name + "第" + this._chapter.Id + "话" + "(" +(this.LookIndex+1)+ "/"+this.ImageCount+")";
		}

		private void Window_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
		{
			if(e.Key == Key.Left || e.Key == Key.P)
			{
				this.Preview();
			}
			
			if(e.Key == Key.Right || e.Key==Key.Space)
			{
				this.Next();
			}
			
			if(e.Key == Key.Escape)
			{
				this.Close();
			}

            if (e.Key == Key.Down)
            {
                this.ScrollViewForPic.ScrollToVerticalOffset(this.ScrollViewForPic.VerticalOffset + this.ScrollViewForPic.ScrollableHeight / 10.0);
            }

            if (e.Key == Key.Up)
            {
                this.ScrollViewForPic.ScrollToVerticalOffset(this.ScrollViewForPic.VerticalOffset- this.ScrollViewForPic.ScrollableHeight / 10.0);
            }
		}

        
      

    }
}
